package com.yiji.fintech.musfinance.common.strategy.gateway;

import com.acooly.core.utils.Ids;
import com.acooly.core.utils.enums.ResultStatus;
import com.acooly.core.utils.mapper.BeanCopier;
import com.yiji.fintech.musfinance.base.enums.ElementTypeEnum;
import com.yiji.fintech.musfinance.base.facade.GatewayOrderFacade;
import com.yiji.fintech.musfinance.base.facade.GatewayResultFacade;
import com.yiji.fintech.musfinance.center.base.result.ResultFacade;
import com.yiji.fintech.musfinance.common.domain.SignDomain;
import com.yiji.fintech.musfinance.common.domain.base.BaseDomain;
import com.yiji.fintech.musfinance.common.factory.gateway.SignFactory;
import com.yiji.fintech.musfinance.common.service.SignUtilService;
import com.yiji.fintech.musfinance.common.strategy.base.AbstractSendGateWayBaseStrategy;
import com.yiji.fintech.musfinance.entity.SignContractno;
import com.yiji.fintech.musfinance.entity.SignInfo;
import com.yiji.fintech.musfinance.service.SignContractnoService;
import com.yiji.fintech.musfinance.service.SignInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 签约发送网关策略
 * Created by ouwen@yiji.com} on 2017/9/27.
 */
@Service
public class SendGateWaySignStrategy extends AbstractSendGateWayBaseStrategy {
    @Autowired
    private SignFactory signFactory;
    @Autowired
    private SignInfoService signInfoService;
    @Autowired
    private SignContractnoService signContractnoService;
    @Autowired
    private SignUtilService signUtilService;
    @Override
    protected GatewayOrderFacade converter(BaseDomain domain) {
        return signFactory.factory(domain);
    }

    @Override
    protected GatewayResultFacade send(BaseDomain domain) {
        //TODO
        //调用网关接口
        GatewayResultFacade resultFacade = new GatewayResultFacade();
        resultFacade.setGid(domain.getGid());
        resultFacade.setMerchOrderNo(domain.getMerchOrderNo());
        resultFacade.setBizOrderNo(domain.getBizOrderNo());
        resultFacade.setPartnerId(domain.getPartnerId());
        resultFacade.setStatus(ResultStatus.success);
        resultFacade.setCode(ResultStatus.success.getCode());
        resultFacade.setContractNo(Ids.oid());//合同号
        resultFacade.setDetail("mock网关签约处理成功");
        return resultFacade;
    }

    @Override
    protected void result(BaseDomain domain, ResultFacade result) {
        //TODO
        //更新返回业务方结果
        BeanCopier.copy(resultGateWay,result,BeanCopier.CopyStrategy.IGNORE_NULL, BeanCopier.NoMatchingRule.IGNORE,null);
        SignDomain signDomain = (SignDomain)domain;
        //更新domain同步流水
        BeanCopier.copy(resultGateWay,signDomain,BeanCopier.CopyStrategy.IGNORE_NULL, BeanCopier.NoMatchingRule.IGNORE,null);
        //根据结果保存签约表
        updateSignInfo(domain);
    }
    /**
     * 更新签约信息表
     * @param domain
     */
    private void updateSignInfo(BaseDomain domain){
        SignDomain signDomain = (SignDomain)domain;
        if(signUtilService.isExsit(signDomain) == null){
            String signInfoId = Ids.oid();
            SignInfo signInfo = new SignInfo();
            signInfo.setSignInfoId(signInfoId);
            signInfo.setCertNo(signDomain.getCertNo());
            signInfo.setAccountName(signDomain.getAccountName());
            signInfo.setAccountNo(signDomain.getAccountNo());
            signInfo.setPhoneNo(signDomain.getPhoneNo());
            signInfo.setElementType(domain.getElementType());
            //保存签约信息
            save(signInfo,signDomain);
        }
    }
    private void save(SignInfo signInfo,SignDomain signDomain){
        if(ElementTypeEnum.FOUR == signInfo.getElementType()){
            String signInfoId = signInfo.getSignInfoId();
            saveFourElement(signInfo);//保存四要素信息
            SignContractno signContractno = new SignContractno();
            signContractno.setSignInfoId(signInfoId);
            signContractno.setGatewayChannelApi(signDomain.getGatewayChannelApi());
            signContractno.setContractNo(signDomain.getContractNo());
            signContractnoService.save(signContractno);//渠道合同号

            saveThreeElement(signInfo);//保存三要素信息
            saveTwoElement(signInfo);//保存二要素
        }else if(ElementTypeEnum.THREE == signDomain.getElementType()){//三要素
            saveThreeElement(signInfo);//保存三要素信息
            saveTwoElement(signInfo);//保存二要素
        }else{//二要素
            saveTwoElement(signInfo);//保存二要素
        }
    }

    /**
     * 保存二要素
     * @param signInfo1
     */
    private void saveTwoElement(SignInfo signInfo1){
        signInfo1.setPhoneNo(null);
        signInfo1.setAccountNo(null);
        signInfo1.setElementType(ElementTypeEnum.TWO);
        signInfoService.save(signInfo1);//保存二要素
    }
    /**
     * 保存三要素
     * @param signInfo1
     */
    private void saveThreeElement(SignInfo signInfo1){
        signInfo1.setPhoneNo(null);
        signInfo1.setElementType(ElementTypeEnum.THREE);
        signInfoService.save(signInfo1);//保存三要素
    }
    /**
     * 保存四要素
     * @param signInfo1
     */
    private void saveFourElement(SignInfo signInfo1){
        signInfo1.setElementType(ElementTypeEnum.FOUR);
        signInfoService.save(signInfo1);//保存四要素
    }
}
